package servlet;

import dao.BlogDao;
import model.Blog;
import utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/4/8 18:59
 */
@WebServlet("/blogdetail")
public class BlogDetailServlet extends HttpServlet {
    BlogDao blogDao = new BlogDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf8");

        String blogId = req.getParameter("blogId");
        System.out.println(blogId);
        if (blogId==null || "".equals(blogId)){
            resp.getWriter().write("");
            return;
        }
        int id = Integer.parseInt(blogId);
        Blog blog = blogDao.getBlogByID(id);
        //todo 如果请求参数不正确，返回列表详情页
        if (blog  == null ){
            resp.getWriter().write("");
            return;
        }
        resp.setContentType("application/json");
        String json= JSONUtils.toJSON(blog);
        resp.getWriter().write(json);
    }
}
